﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using LinqToPi;

namespace LinqToPiWCFTest.Web
{
    // NOTE: If you change the class name "PiDataService" here, you must also update the reference to "PiDataService" in Web.config.
    public class PiDataService : IPiDataService
    {
        static PiServerContext context;

        static PiDataService()
        {
            context = new PiServerContext("nlms028.nl.int.atosorigin.com", "piadmin", Console.Out);
        }

        public IEnumerable<PiLogItem> SearchPiMessageLog(string contains, DateTime startTime, DateTime endTime)
        {
            return context.System.MessageLog.Where(c => c.Message.ToUpper().Contains(contains.ToUpper()) && c.Time.Between(startTime, endTime));
        }

        public IEnumerable<PiValue> GetPiValues(string tagName, DateTime startTime, DateTime endTime)
        {
            var ret = context.Archive.ArchiveData.Where(c => c.Tag == tagName && c.Time.Between(endTime, startTime)).ToList();
            return ret;
        }

        public bool UpdatePiValue(PiValue value)
        {
            try
            {
                context.Archive.ArchiveData.Update(value);
                //context.Archive.ArchiveData.
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
